package com.dff.jrfw.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dff.jrfw.InputCodeActivity;
import com.dff.jrfw.R;
import com.dff.jrfw.remote.CustomCallBack;
import com.dff.jrfw.remote.RetrofitUtilsTwo;
import com.dff.jrfw.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class CertOutTimeDialog extends Dialog {

    private TextView sure;
    private String username;
    private Activity context;

    public CertOutTimeDialog(@NonNull Activity context, String username) {
        super(context, R.style.CustomDialog);
        this.context = context;
        this.username = username;
        setCanceledOnTouchOutside(false);   // 点击其他地方dialog不消失
        setCancelable(false);         // 点击返回键dialog不消失
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_cert_out_time_layout);
        sure = findViewById(R.id.tv_sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                doAccountIsAlive(username);
            }
        });
    }


    private void doAccountIsAlive(String username) {
        RetrofitUtilsTwo.getService().doAccountIsAlive("1", "1", username).enqueue(new CustomCallBack<ResponseBody>() {

            @Override
            public void onFailure(Call call, Throwable t) {
                super.onFailure(call, t);
            }

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.body() != null) {
                        String result = response.body().string();
                        JSONObject jsonObject = new JSONObject(result);
                        int code = jsonObject.getInt("code");
                        if (code == 200) {
                            JSONObject data = jsonObject.optJSONObject("data");
                            String mobile = data.optString("mobile");
                            InputCodeActivity.callMe(context, username, mobile, "loginTwo");
                        } else {
                            String msg = jsonObject.getString("msg");
                            ToastUtil.toast(msg);
                        }
                    } else {
                        ResponseBody errorResponseBody = response.errorBody();
                        if (errorResponseBody != null) {
                            ToastUtil.toast(errorResponseBody.string());
                        }
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
